/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.cache.JedisUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtMle;
import com.jeesite.modules.goods.entity.ZtSk;
import com.jeesite.modules.goods.entity.ZtTip;
import com.jeesite.modules.goods.service.ZtTipService;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztTip")
public class ZtTipController extends BaseController {

	@Autowired
	private ZtTipService ztTipService;

	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtTip get(String id, boolean isNewRecord) {

		ZtTip ztTip = new ZtTip();
		ztTip.setId(id);
		ztTip = ztTipService.get(ztTip);
		if (ztTip == null) {
			ztTip = new ZtTip();
		}
		return ztTip;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztTip:ztTipData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtTip ztTip, Model model) {
		model.addAttribute("ztTip", ztTip);

		return "modules/ztTip/ztTipList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztTip:ztTipData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtTip> listData(ZtTip ztTip,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {

		Page<ZtTip> page = ztTipService.findPage(new Page<ZtTip>(
				request, response), ztTip);

	

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztTip:ztTipData:view")
	@RequestMapping(value = "form")
	public String form(ZtTip ztTip, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("ztTip", ztTip);

		return "modules/ztTip/ztTipForm";
	}
	
	@RequiresPermissions("sk:skData:edit")
	@RequestMapping(value = "setSk")
	public String setSk(Model model,
			HttpServletRequest request, HttpServletResponse response) {
		ZtSk ztSk = new ZtSk();
		if(JedisUtils.getObject("ztSkContro")!=null){
			 ztSk=(ZtSk) JedisUtils.getObject("ztSkContro");
		}else{
			ztSk=new ZtSk();
		}
		
		model.addAttribute("ztSk", ztSk);

		return "modules/ztTip/skForm";
	}
	
	
	@PostMapping(value = "saveSk")
	@ResponseBody
	public String saveSk(ZtSk ztSk) {
		JedisUtils.setObject("ztSkContro", ztSk, 0);
		
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	
	@RequiresPermissions("mle:mleData:edit")
	@RequestMapping(value = "setMle")
	public String setMle(Model model,
			HttpServletRequest request, HttpServletResponse response) {
		ZtMle ztMle = new ZtMle();
		if(JedisUtils.getObject("ztMleContro")!=null){
			ztMle=(ZtMle) JedisUtils.getObject("ztMleContro");
		}else{
			ztMle=new ZtMle();
		}
		ZtSk ztSk = new ZtSk();
		if(JedisUtils.getObject("ztSkContro")!=null){
			 ztSk=(ZtSk) JedisUtils.getObject("ztSkContro");
		}else{
			ztSk=new ZtSk();
		}
		model.addAttribute("ztSk", ztSk);
		
		model.addAttribute("ztMle", ztMle);

		return "modules/ztTip/mleForm";
	}
	
	
	@PostMapping(value = "saveMle")
	@ResponseBody
	public String saveMle(ZtMle ztMle) {
		JedisUtils.setObject("ztMleContro", ztMle, 0);
		
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	
	
	
	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztTip:ztTipData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtTip ztTip) {
		if(StringUtils.isBlank(ztTip.getId())){
			ztTip.setCreateDate(new Date());
		}
		ztTipService.save(ztTip);
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztTip:ztTipData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtTip ztTip) {
		ztTipService.delete(ztTip);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
}